package com.atguigu.tingshu.user.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author GQS @Date 2024/11/3/21:30 @Description
 * 用户初次登录 初始化账户 使用的消息队列的配置: 懒汉模式 不发送消息,不创建这些 若已存在,也不创建
 */
@Configuration
public class UserAndAccountRabbitConfig {
  /**
   * 创建交换机
   */
    @Bean("userAndAccountExchange")
    public Exchange userAndAccountExchange(){
      return ExchangeBuilder.directExchange("user_and_account_exchange").build();
    }

  /**
   * 创建队列
   */
  @Bean("userAndAccountQueue")
  public Queue userAndAccountQueue(){
    return QueueBuilder.durable("user_and_account_queue").build();
  }

  /**
   * 创建绑定关系
   */
  @Bean
  public Binding userAndAccountQueueBinding(@Qualifier("userAndAccountQueue") Queue userAndAccountQueue,
                                            @Qualifier("userAndAccountExchange") Exchange userAndAccountExchange){
    return BindingBuilder
            .bind(userAndAccountQueue)
            .to(userAndAccountExchange)
            .with("user.account")
            .noargs();
  }
}